<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<head>
<script src="/js/jquery-3.5.1.min.js"></script>
</head>

<div align="center" style="margin-top: 100px;">
    <h2>注册</h2>

    <p>
        <form action="/register" method="post">
             <table align="center">
                    <tr>
                        <td >用户名</td>
                        <td ><input id="name" name="name" placeholder="设置您的用户名" > </td>
                    </tr>

                    <tr>
                        <td >登陆密码</td>
                        <td ><input id="password" name="password" type="password"  placeholder="设置您的登陆密码" > </td>
                    </tr>

                    <tr>
                        <td >密码确认</td>
                        <td ><input id="repeatpassword" type="password"   placeholder="请再次输入您的密码" > </td>
                    </tr>

                    <tr>
                        <td>
                            <button id="registerBtn" type="submit">注   册</button>
                        </td>
                    </tr>
              </table>
        </form>
    </p>
</div>

<script type="text/javascript">

    $(document).ready(function(){

        $("#registerBtn").click(function(){
           var msg = "";
           var nameValue = $("#name").val();
           if(!nameValue){
               msg += "用户名不能为空！"+"\n";
           }

           var psw = $("#password").val();
           if(!psw){
               msg += "密码不能为空！" + "\n";
           }

           var repPsw = $("#repeatpassword").val();
           if(!repPsw){
               msg += "请再次输入密码！" + "\n";
           }

           if(msg != ""){
               alert(msg);
               return false;
           }

            res = "";
            $.ajax({
                type :"post",
                url :"/userExists",
                data :{ name : nameValue },
                async :false,
                success :function(data){
                    res = data;
                }});
             if(res == "true"){
                 alert("用户 '" + nameValue + "' 已存在！请注册其他用户名！");
                 return false;
             }


        });

    });

</script>
